#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <functional>
#include <time.h>
#include "mutex.hpp" 
#include "cond.hpp"
#include "blockqueue.hpp"
#include "task.hpp"
using task_t = std::function<void()>;
using namespace myblockqueue;
using namespace TaskModule;
void test()
{
    std::cout << "test..................." << std::endl;
}

void *consumer(void *arg)
{
    //BlockQueue<task_t> *bq = static_cast<BlockQueue<task_t> *>(arg);
    BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(arg);
    while (1)
    {
        //task_t t;
        Task t;
        bq->dequeue(&t);
        t.Excute();
    }
    return nullptr;
}
void *producer(void *arg)
{

   // BlockQueue<task_t> *bq = static_cast<BlockQueue<task_t> *>(arg);
   BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(arg);
    while (1)
    {
        //bq->equeue(&test);
        int x = rand() % 10 + 1; // [1, 10]
        int y = rand() % 20 + 1; // [1, 20]
        Task t(x, y); // 构建任务
        bq->equeue(t);
        sleep(1);
    }
    return nullptr;
}
int main()
{

    srand(time(nullptr)^getpid());
    //BlockQueue<task_t> bq(5);
    BlockQueue<Task> bq(5);

    pthread_t tid1, tid2,tid3,tid4;
    pthread_create(&tid1,nullptr,consumer,&bq);
    pthread_create(&tid4,nullptr,consumer,&bq);
    pthread_create(&tid3,nullptr,consumer,&bq);

    pthread_create(&tid2,nullptr,producer,&bq);
    pthread_join(tid1,nullptr); 
    pthread_join(tid2,nullptr);


    return 0;
}